define("login", ["jquery", "md5", "jquery.validate", "additional-methods", "jquery.idcode", "jquery.cookie"], function () {
    return {
        login: function () {
            var obj = JSON.parse($.cookie("user")||'[]')
            $("[name=uname]").val(obj.uname)
            //验证码
            $.idcode.setCode();
            $("#yzbtn").on("click", function () {
                $.idcode.setCode();
                return false
            })

            //表单验证

            $("form").validate({
                rules: {
                    uname: {
                        required: true,
                    },
                    upwd: {
                        required: true,
                    },
                },
                messages: {
                    uname: {
                        required: "请输入用户名",
                    },
                    upwd: {
                        required: "密码不能为空",
                    },
                },
                submitHandler: function () {
                    //验证码检查
                            var IsBy = $.idcode.validateCode()
                            if (IsBy) {
                                $("#yzmsg").html("验证码正确")
                                if ($("#bxty").prop("checked")) {
                                    var data = {
                                        uname: $("[name=uname]").val(),
                                    }
                                    $.cookie("user", JSON.stringify(data), {
                                        expires: 7
                                    })
                                }
                                $.ajax({
                                    url: "php/login.php",
                                    type: "post",
                                    data: {
                                        uname: $("[name=uname]").val(),
                                        upwd: $.md5($("[name=upwd]").val())
                                    }
                                }).then(function (res) {
                                    if (res.state == "1") {
                                        var str=document.referrer
                                        var arr=str.split("/")
                                        var arrs=arr[arr.length - 1].split("?")

                                        //判断上一个页面
                                        if(arrs[0]=="register.html"||arr[0]==""){
                                            $(".success").css("display", "block")
                                            $(".success span").html("登录成功")
                                            $(".success b").html("系统即将跳转主页")
                                            setTimeout(function () {
                                                window.location="index.html"
                                            }, 3000)
                                        }else {
                                            $(".success").css("display", "block")
                                            $(".success span").html("登录成功")
                                            $(".success b").html("系统即将跳转到登录前的页面")
                                            setTimeout(function () {
                                                window.location=document.referrer
                                            }, 2000)
                                        }


                                    } else {
                                        $(".success").css("display", "block")
                                        $(".success span").html("登录失败")
                                        $(".success b").html("用户名或密码错误")
                                        setTimeout(function () {
                                            $(".success").css("display", "none")
                                        }, 2000)
                                    }
                                })

                            } else {
                                $("#yzmsg").html("验证码错误")
                            }


                    return false
                }
            })

        }
    }
})